import { Component, OnInit, Output, EventEmitter, Input, forwardRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { RecipesManageService } from '../../../../shared/services/recipes.service';
import { ChannelManageService } from '../../../../shared/services/channel.service';
import { LabelManageService } from '../../../../shared/services/label.service';
import { MaterialManageService } from '../../../../shared/services/material.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-detail-grounding',
  templateUrl: './detail-grounding.component.html',
  styleUrls: ['./detail-grounding.component.scss']
})
export class DetailGroundingComponent implements OnInit {

  singleValue = true;

  groundingParams: any;

  groundingType: Boolean = false;

  upType: any;

  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private _message: NzMessageService,
    private materialManageService: MaterialManageService,
    private labelManageService: LabelManageService,
    private channelManageService: ChannelManageService,
    private recipesManageService: RecipesManageService,
    private fb: FormBuilder
  ) { }

  ngOnInit() {
    this.reset();
    // 解析获得参数导入
    this.route.queryParams
        .subscribe((pam: any) => {
          // console.log('grounding', pam);
          this.upType = pam['grounding'] === 'edit' ? 'edit' : 'see';
          this.groundingParams.id = pam['id'];
          this.recipesManageService.getRecipe(pam['id']).subscribe(res => {
            if (res['code'] === '0') {
              this.groundingParams.pubStatus = res['data']['pubStatus'];
              if (pam['type'] === 'see' ) {
                this.groundingType = false;
              }
              if (pam['type'] === 'edit' || pam['type'] === 'create') {
                this.groundingType = true;
              }
            }
          });
        });
  }

  reset() {
    this.groundingParams = {
      id: '',
      pubStatus: '',      // 食谱上下架状态， 未上架，上架
    };
  }

  saveGrounding() {
    // console.log(this.groundingParams);
    this.recipesManageService.postUpdateRecipesPubStatus(this.groundingParams).subscribe(res => {
      // console.log(res);
      if (res['code'] === '0') {
        this._message.success('上架信息已更新', {nzDuration: 2000});
        this.goback();
      } else {
        this._message.error('上架信息更新失败,message:' + res['msg'], {nzDuration: 3000});
      }
    });
  }

  goback() {
    this.router.navigate(['../'], { relativeTo: this.route, queryParamsHandling: 'preserve' });
  }

}
